<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'data'.DS.'DAOPoi.php');
require_once('FTConnection.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class FTDAOPoi implements DAOPoi{
	
	private $_connection = null;
	private $_poiFTID;
	
	public function __construct(){
		$this->_connection=FTConnection::getInstance();
		$this->getPOIFTId();
	}
	
	private function getPOIFTId(){
		$daoPOIConfig=JDAOFactory::getInstance()->getDAOFTPOIConfig();
		$this->_poiFTID=$daoPOIConfig->getFTPOIConfig();
	}
	
	private function getPOIRowId($id){
		$row=$this->_connection->query("SELECT ROWID FROM ".$this->_poiFTID->getPOIFTId()." WHERE id=".$id);
		return $row[0]['rowid'];
	}
	
	public function getRoutePOIs($id){
		$rows=$this->_connection->query("SELECT * FROM ".$this->_poiFTID->getPOIFTId()." WHERE routeID=".$id);
		return DTOFactory::getInstance()->getDTOPOIsFromArray($rows);
	}
	
	public function savePOI($poi){
		return $this->_connection->query("INSERT INTO ".$this->_poiFTID->getPOIFTId().
								  " (id,routeID,subCategoryID,name,description,location) VALUES (".
								  $poi->getId().",".
								  $poi->getRoute().",".$poi->getSubCategory().",'".$poi->getName()."','".
								  $poi->getDescription()."','".$poi->getRawLocation()."')");
	}
	
	public function updatePOI($poi){
		$rowID=$this->getPOIRowId($poi->getId());
		return $this->_connection->query("UPDATE ".$this->_poiFTID->getPOIFTId().
								  " SET name='".$poi->getName()."',
								  description='".$poi->getDescription()."',
								  location='".$poi->getRawLocation()."',
								  subCategoryID='".$poi->getSubCategory()."' WHERE ROWID='".$rowID."'");
	}
	
	public function deletePOI($id){
		$rowID=$this->getPOIRowId($id);
		return $this->_connection->query("DELETE FROM ".$this->_poiFTID->getPOIFTId()." WHERE ROWID='".$rowID."'");
	}
	
}

?>